Sponsored Content
Full Discussion: Help with multithreading
Top Forums Programming Help with multithreading Post 302749187 by Corona688 on Thursday 27th of December 2012 05:43:43 PM
Old 12-27-2012
What they're talking about with the pointer is, whenever you create a thread, you are giving it a pointer to something. The function "called" by the new thread has that value passed into it -- that's why it takes any value at all, instead of being void.

It can be whatever you want, and is always there. If you're not passing data into it, you're probably just giving it NULL. If you wanted to pass each thread something different, be sure to not give them the same variable -- they'll all end up pointing to the same thing, they won't get unique values. To be different it needs to literally be different memory. You could give them different elements of an array, for example. Memory fresh from malloc() also works.

Of course, threads have their own unique ID's in the first place, so it's a bit redundant to give it your own unique number, but that's why this is an exercise.
 

9 More Discussions You Might Find Interesting

1. Programming

Multithreading in Pro*C

:confused: Hi! I have created a Multhreaded Application in Pro*C (using pthreads) with about 5 Threads running simultaneously. The Application is basically to Update a Centralized Table in Oracle, which updates different rows in the Table (Each Thread updates different rows!). The... (16 Replies)
Discussion started by: shaik786
16 Replies

2. Programming

multithreading on OSX

Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function. void update() { thread1->update(); // fluid solver ... (3 Replies)
Discussion started by: memoid
3 Replies

3. UNIX for Advanced & Expert Users

multithreading in UNIX

Hi, Can you please give me a suitable reference to learn multithreading programming in C in UNIX? Thanks (3 Replies)
Discussion started by: naan
3 Replies

4. Shell Programming and Scripting

Multithreading program

Hi I need to insert 1million records into MySQL database, but it is taking lot of time as there is no bulk insert support. I want to spawn 10 processes which will insert 100k records each parallely. Can somebody help me with a example program to execute this task through shell scripting. (5 Replies)
Discussion started by: sach_roger
5 Replies

5. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

6. IP Networking

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (0 Replies)
Discussion started by: moti12
0 Replies

7. Programming

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (6 Replies)
Discussion started by: moti12
6 Replies

8. Programming

Multithreading in reading file

Dear all, I am having a huge XML file, as below structure <EMPLOYEE> <RECORD id =aaa> <Salary>99999</Salary> <section>ssss</section> </RECORD> <RECORD id =bbb> <Salary>77777</Salary> <section>ssss</section> </RECORD> </EMPLOYEE> This is a 50 GB file I want to read this file in... (9 Replies)
Discussion started by: arunkumar_mca
9 Replies

9. What is on Your Mind?

Alarm interrupt and multithreading

Hi Friends any know how became a friend in this Android Programming Language (0 Replies)
Discussion started by: ljarun
0 Replies
thr_keycreate(3C)														 thr_keycreate(3C)

NAME
thr_keycreate, thr_setspecific, thr_getspecific - thread-specific data functions SYNOPSIS
cc -mt [ flag... ] file...[ library... ] #include <thread.h> int thr_keycreate(thread_key_t *keyp, void (*destructor)(void *)); int thr_setspecific(thread_key_t key, void *value); int thr_getspecific(thread_key_t key, void **valuep); Create Key In general, thread key creation allocates a key that locates data specific to each thread in the process. The key is global to all threads in the process, which allows each thread to bind a value to the key once the key has been created. The key independently maintains specific values for each binding thread. The thr_keycreate() function allocates a global key namespace, pointed to by keyp, that is visible to all threads in the process. Each thread is initially bound to a private element of this key, which allows access to its thread-specific data. Upon key creation, a new key is assigned the value NULL for all active threads. Additionally, upon thread creation, all previously created keys in the new thread are assigned the value NULL. Optionally, a destructor function destructor can be associated with each key. Upon thread exit, if a key has a non-null destructor func- tion and the thread has a non-null value associated with that key, the destructor function is called with the current associated value. If more than one destructor exists for a thread when it exits, the order of destructor calls is unspecified. Set Value Once a key has been created, each thread can bind a new value to the key using thr_setspecific(). The values are unique to the binding thread and are individually maintained. These values continue for the life of the calling thread. Proper synchronization of key storage and access must be ensured by the caller. The value argument to thr_setspecific() is generally a pointer to a block of dynamically allocated memory reserved by the calling thread for its own use. See EXAMPLES below. At thread exit, the destructor function, which is associated at time of creation, is called and it uses the specific key value as its sole argument. Get Value thr_getspecific() stores the current value bound to key for the calling thread into the location pointed to by valuep. If successful, thr_keycreate(), thr_setspecific() and thr_getspecific() return 0. Otherwise, an error number is returned to indicate the error. If the following conditions occur, thr_keycreate() returns the corresponding error number: EAGAIN The system lacked the necessary resources to create another thread-specific data key. ENOMEM Insufficient memory exists to create the key. If the following conditions occur, thr_keycreate() and thr_setspecific() return the corresponding error number: ENOMEM Insufficient memory exists to associate the value with the key. The thr_setspecific() function returns the corresponding error number: EINVAL The key value is invalid. Example 1: Call the thread-specific data from more than one thread without special initialization. In this example, the thread-specific data in this function can be called from more than one thread without special initialization. For each argument passed to the executable, a thread is created and privately bound to the string-value of that argument. /* cc -mt thisfile.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <thread.h> void *thread_specific_data(void *); void cleanup(void*); #define MAX_ARGC 20 thread_t tid[MAX_ARGC]; int num_threads; int main(int argc, char *argv[]) { int i; num_threads = argc - 1; for (i = 0; i < num_threads; i++) thr_create(NULL, 0, thread_specific_data, argv[i+1], 0, &tid[i]); for (i = 0; i < num_threads; i++) thr_join(tid[i], NULL, NULL); return(0); } /* end main */ void * thread_specific_data(void *arg) { static mutex_t keylock; /* static ensures only one copy of keylock */ static thread_key_t key; static int once_per_keyname = 0; char *private_data = arg; void *tsd = NULL; void *data; if (!once_per_keyname) { mutex_lock(&keylock); if (!once_per_keyname) { thr_keycreate(&key, cleanup); once_per_keyname++; } mutex_unlock(&keylock); } thr_getspecific(key, &tsd); if (tsd == NULL) { data = malloc(strlen(private_data) + 1); strcpy(data, private_data); thr_setspecific(key, data); thr_getspecific(key, &tsd); } printf("tsd for %d = %s ", thr_self(), (char *)tsd); thr_getspecific(key, &tsd); printf("tsd for %d remains %s ", thr_self(), (char *)tsd); return (NULL); } /* end thread_specific_data */ void cleanup(void *v) { /* application-specific clean-up function */ free(v); } See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Stable | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ thr_exit(3C), attributes(5), standards(5) WARNINGS
The thr_getspecific() and thr_getspecific() functions can be called either explicitly or implicitly from a thread-specific data destructor function. Calling thr_setspecific() from a destructor can result in lost storage or infinite loops. 1 Sep 2005 thr_keycreate(3C)
All times are GMT -4. The time now is 06:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy