Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am planning to pass value "1" and "4" to the smoker_tobacco but my output shows
ubuntu@ubuntu:~/Desktop$ ./testing
140243696023296,140243670845184
Segmentation fault (core dumped)


Much appreciated for the help.

What I have tried:

C++
void *smoker_tobacco (void *id)
{
	int* tid = (int*)id;
    	cout << "Thread id:"<< *tid << endl;
}


int main(){

	pthread_t smoker1,smoker4;
	
	sem_init(&table,0,1);
	sem_init(&sem_tobacco,0,0);
	sem_init(&sem_paper,0,0);
	sem_init(&sem_match,0,0);
	
	pthread_create(&smoker1, NULL, smoker_tobacco, (void*)1);
	pthread_create(&smoker4, NULL, smoker_tobacco, (void*)4);

	cout<<smoker1<<","<<smoker4<<endl;
	
	pthread_join(smoker1,NULL);
	pthread_join(smoker4,NULL);

	
	return 0;
	
}
Posted
Updated 19-Jul-22 5:34am
v2

You can cast from int to void* and back again, without any problems:
C++
#include <iostream>

void f(void *p)
{
    int i = reinterpret_cast<intptr_t>(p);
    std::cout << "f: i = " << i << '\n';
}

int main()
{
    f( reinterpret_cast<void*>(1) );

    return 0;
}
Note that this when going from void* to int we have to cast the pointer to inptr_t, otherwise, in 64 bit mode we get an error message regarding the cast from void* to int looses precision.
In this case, this is reasonably safe, since we know that sizeof(void*) >= sizeof(int), so any int can be represented in a void*.
 
Share this answer
 
Comments
Maciej Los 19-Jul-22 11:48am    
5ed!
The correct way to retrieve the passed parameter follows
C++
void *smoker_tobacco (void *id)
{
  long tid = (long)id;
  cout << "Thread id:"<< tid << endl;
  return (void *) 0;
}

Try, for instance
C++
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using namespace std;


void *smoker_tobacco (void *id)
{
  long tid = (long)id;
  cout << "Thread id:"<< tid << endl;
  return (void *) 0;
}


int main()
{
  pthread_t smoker1,smoker4;
  sem_t table, sem_tobacco, sem_paper, sem_match;

  sem_init(&table,0,1);
  sem_init(&sem_tobacco,0,0);
  sem_init(&sem_paper,0,0);
  sem_init(&sem_match,0,0);

  pthread_create(&smoker1, NULL, smoker_tobacco, (void*)1);
  pthread_create(&smoker4, NULL, smoker_tobacco, (void*)4);

  cout<<smoker1<<","<<smoker4<<endl;

  pthread_join(smoker1,NULL);
  pthread_join(smoker4,NULL);

  return 0;
}
 
Share this answer
 
Comments
cyye 19-Jul-22 11:30am    
It works! Thanks very much!!
0x01AA 19-Jul-22 11:35am    
+5. Don't bogart that joint my friend, pass it over to me ... :-)
jeron1 19-Jul-22 12:10pm    
Ha! Classic tune!
CPallini 19-Jul-22 16:40pm    
:-D
Maciej Los 19-Jul-22 11:48am    
5ed!
You are casting the values 1 and 4 to void*. But they are not valid addresses, hence the Segnmentation Fault. You should pass (and accept) simple values, or create proper pointers.

C++
int value1 = 1;
int value4 = 4;
	pthread_create(&smoker1, NULL, smoker_tobacco, (void*)&value1);
	pthread_create(&smoker4, NULL, smoker_tobacco, (void*)&value4);
 
Share this answer
 
Comments
CPallini 19-Jul-22 11:18am    
The OP passes the parameters correctly (yes, you can do that). The flawn is on parameter retrieval.
Richard MacCutchan 19-Jul-22 11:32am    
Yes, I am well aware of that, but I decided to stick with a simpler solution.
CPallini 19-Jul-22 11:35am    
Simpler?! :-)
Richard MacCutchan 19-Jul-22 11:37am    
For someone who is obviously new and does not fully understand casts; yes.
CPallini 19-Jul-22 11:40am    
Well, it works!
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900