simple pthread

C

cerr

hi There,

I'm just trying to write a little sample program with a thread. I can't getmy thread started and am not sure why, I get
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:59: error: expected primary-expression before ‘void’
example.cpp:42:63: error: initializer expression list treated as compound expression
the code:
int MyThread::StartMe(void)
{
int pthread_create(&ThreadA, NULL, &MyThread::printMsg, void);
}
//-------------------------------------------------------------

void MyThread::printMsg(void)
{
cout << "I Incremented count: " << count << endl;
}

Can anyone point it out? I'm like HUH???

Thanks,
 
R

red floyd

hi There,

I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:59: error: expected primary-expression before ‘void’
example.cpp:42:63: error: initializer expression list treated as compound expression
the code:
int MyThread::StartMe(void)
{
int pthread_create(&ThreadA, NULL,&MyThread::printMsg, void);
get rid of "int"
 
I

Ian Collins

hi There,

I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:59: error: expected primary-expression before ‘void’

void is a type, you can't pass a type as a parameter!
example.cpp:42:63: error: initializer expression list treated as compound expression
the code:
int MyThread::StartMe(void)
{
int pthread_create(&ThreadA, NULL,&MyThread::printMsg, void);

This looks like a mixed up function declaration!


int something = pthread_create( );

A decent compiler should tell you that MyThread::printMsg is the wrong
function type for the function pointer parameter of pthread_create.
 
A

Alf P. Steinbach

I'm just trying to write a little sample program with a thread. I can't get my thread started and am not sure why, I get
$ g++ -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:42:59: error: expected primary-expression before ‘void’
example.cpp:42:63: error: initializer expression list treated as compound expression
the code:
int MyThread::StartMe(void)
{
int pthread_create(&ThreadA, NULL,&MyThread::printMsg, void);

`void` is a type.


Cheers & hth.,

- Alf
 
C

cerr

`void` is a type.

Hi Alf, yep I've figured that out by now but passing NULL doesn't work
either, how do I pass a void argument when my thread function is
declared like: void PrintMsg(void);?
Thanks!
 
C

cerr

i have now something like this:
int MyThread::StartMe(void)
{
// starting thread
pthread_create(&ThreadA, NULL, &PrintMsg, NULL);

}
//-------------------------------------------------------------

void PrintMsg(void)
{
//thread function
while (1) {

pthread_mutex_lock( &mutex1 );
count++;
pthread_mutex_unlock( &mutex1 );

cout << "I Incremented count: " << count << endl;
}
}
but I get following:

$ g++ -pthread -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
‘void* (*)(void*)’
example.cpp:66:49: error: initializing argument 3 of ‘int
pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
void*)’

How do I gwet this compiled? How do I pass a void argument?
Thank you!
 
I

Ian Collins

i have now something like this:
int MyThread::StartMe(void)
{
// starting thread
pthread_create(&ThreadA, NULL,&PrintMsg, NULL);

}
//-------------------------------------------------------------

void PrintMsg(void)
{
//thread function
while (1) {

pthread_mutex_lock(&mutex1 );
count++;
pthread_mutex_unlock(&mutex1 );

cout<< "I Incremented count: "<< count<< endl;
}
}
but I get following:

$ g++ -pthread -o example example.cpp
example.cpp: In member function ‘int MyThread::StartMe()’:
example.cpp:66:49: error: invalid conversion from ‘void (*)()’ to
‘void* (*)(void*)’
example.cpp:66:49: error: initializing argument 3 of ‘int
pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*),
void*)’

How do I gwet this compiled? How do I pass a void argument?
Thank you!

Pass the the correct function type!

extern "C" void* PrintMsg(void*)
 
I

Ian Collins

"io_x"<[email protected]> ha scritto nel messaggio

what do you think about this?
It is a good way for threads?

#include<stdio.h>
#include<iostream.h>
#include<windows.h>

unsigned count=0;
unsigned index=-1;
unsigned array[256];

#define ini InitializeCriticalSection

NO!
 
I

Ian Collins

-------------------
i modify somthing; in printmsg i impose count<100000

unsigned count=0;

void* PrintMsg(void* unused)

A C library expects a C linkage function pointer, so give it one.
{(void) unused;

You don't need the awful Cism in C++, just leave the parameter unnamed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top