473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

PThread library

I would like to ask that PrintHello accepts a void pointer but how come you can equate it to an integer directly ???
" tid = (int)threadid; "
while at the same time instead if a pointer a normal variable was passed into this function
" pthread_create(&threads[t], NULL, PrintHello, (void *)t); "

the above construct is taken from
https://computing.llnl.gov/tutorials...ssingArguments

Expand|Select|Wrap|Line Numbers
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #define NUM_THREADS 5
  4.  
  5. void *PrintHello(void *threadid)
  6. {
  7. int tid;
  8. tid = (int)threadid;
  9. printf("Hello World! It's me, thread #%d!\n", tid);
  10. pthread_exit(NULL);
  11. }
  12.  
  13. int main (int argc, char *argv[])
  14. {
  15. pthread_t threads[NUM_THREADS];
  16. int rc, t;
  17. for(t=0; t<NUM_THREADS; t++){
  18. printf("In main: creating thread %d\n", t);
  19. rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
  20. if (rc){
  21. printf("ERROR; return code from pthread_create() is %d\n", rc);
  22. exit(-1);
  23. }
  24. }
  25. pthread_exit(NULL);
  26. }
  27.  
  28.  
Jan 27 '09 #1
4 2399
JosAH
11,448 Expert 8TB
@digi123
When you create a thread you can pass the address of a thread local chunk of memory to the newly created thread. The author of the example code wanted to have a cheap thread identification so s/he passed a simple integer.

A struct with an int containing the thread id could've been passed instead but this code saves a few lines I guess. Note that the conversion from an int to a void pointer (and vice versa) is implementation defined.

kind regards,

Jos
Jan 27 '09 #2
thanks i got my answer ------
i hope you meant to say 'undefined' i guess
Jan 30 '09 #3
JosAH
11,448 Expert 8TB
@digi123
Nope, I meant "implementation defined", i.e. an implementation can convert between the two types however it pleases but it should do it consistently.

kind regards,

Jos
Jan 31 '09 #4
thank you sir,
i got whole new perspective to look at
Feb 1 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Avin | last post by:
Hi, I am using the Red Hat Linux 9.0 & gcc 3.2 I am trying to make multithreaded library from my c/c++ source code. Do I have to use -pthread gcc option during compile time, or this option is...
7
by: lokb | last post by:
Hi, I am creating a detach thread as shown below and caling pthread_create in a while loop where the file names in the directory are fetched and is passed as a parmater to pthread create. The...
9
by: nan.li.g | last post by:
Hello, all, I have an interesting problem about stl map and pthread on Linux and g++. The source code is as follows. //mt_map_test.cpp #include <string> #include <map> #include <unistd.h>...
10
by: david wolf | last post by:
The following pthread code cannot compile, I want to know why. Can somebody give me direction on this? if I replace the following line of code status1 = pthread_create(&threadID1, NULL, read,...
2
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in...
2
by: chuckles | last post by:
Hullo all, as the subject says Does pthread emit a sigchld signal on successful exit? I have an app which starts a thread in response to a button press in a GUI. I can't join the thread but I need...
10
by: noleander | last post by:
I've got an application that uses Pthread to do threading. Mostly Im using Condition Variables and the associated function calls: - pthread_cond_wait() - pthread_cond_signal() -...
3
by: Leon | last post by:
Hey, i am trying to develop a game, and I am using OpenGL and pthread. (on Ubuntu, so i also use the X library.) Now i have a problem. I have initialized OpenGL in the main thread, and i want to...
1
by: Akira Kitada | last post by:
Hi, I'm running Python 2.5 on FreeBSD 4. pthread on FreeBSD 4 has some problems so I would like to build python with lthread (linuxthreads) instead of BSD's pthread. So I looked at configure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.