#define _MULTI_THREADED
#include <stdio.h>
#include <time.h>
#include <pthread.h>
/* For safe condition variable usage, must use a boolean predicate and */
/* a mutex with the condition. */
int workToDo = 0;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define NTHREADS 3
#define WAIT_TIME_SECONDS 15
void *threadfunc(void *parm)
{
int rc;
struct timespec ts;
struct timeval tp;
rc = pthread_mutex_lock(&mutex);
//check return code
/* Usually worker threads will loop on these operations */
while (1) {
rc = gettimeofday(&tp, NULL);
/* Convert from timeval to timespec */
ts.tv_sec = tp.tv_sec;
ts.tv_nsec = tp.tv_usec * 1000;
ts.tv_sec += WAIT_TIME_SECONDS;
while (!workToDo) {
printf("Thread blocked\n");
rc = pthread_cond_timedwait(&cond, &mutex, &ts);
/* If the wait timed out, in this example, the work is complete, and */
/* the thread will end. */
/* In reality, a timeout must be accompanied by some sort of checking */
/* to see if the work is REALLY all complete. In the simple example */
/* we will just go belly up when we time out. */
if (rc == ETIMEDOUT) {
printf("Wait timed out!\n");
rc = pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
}
printf("Thread consumes work here\n");
workToDo = 0;
}
rc = pthread_mutex_unlock(&mutex);
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
int i;
pthread_t threadid[NTHREADS];
printf("Enter Testcase - %s\n", argv[0]);
printf("Create %d threads\n", NTHREADS);
for(i=0; i<NTHREADS; ++i) {
rc = pthread_create(&threadid[i], NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
}
rc = pthread_mutex_lock(&mutex);
printf("One work item to give to a thread\n");
workToDo = 1;
rc = pthread_cond_signal(&cond);
rc = pthread_mutex_unlock(&mutex);
printf("Wait for threads and cleanup\n");
for (i=0; i<NTHREADS; ++i) {
rc = pthread_join(threadid[i], NULL);
}
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
printf("Main completed\n");
return 0;
}
Thứ Ba, 25 tháng 2, 2014
An example of Pthread wait condition
Thứ Tư, 30 tháng 1, 2013
Global variable and static variable in C/C++
Source: http://bytes.com/topic/c/answers/860211-global-variable-static-global-variable
The meaning of the "static" keyword in C depends on whether or not it appears within a brace-delimited block. Refer to the following code snippet.
Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only accessible from within that block. In this case, the "static" keyword has the effect of making variable 'd' persist for the life of the program. Variable 'c' only persists while execution is within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the same location for successive executions of the brace-delimited block.
C++ is consistent with C as far as the static keyword is concerned. However, in C++ the static keyword is largely deprecated and is in the language generally for backwards compatibility with C. There remain some uses of it, however.
No one has mentioned linkage so far, which is odd. Linkage comes in two flavours, external and internal. External linkage means the variable or function is accessible (shareable) from outside the compilation unit. Internal linkage means the variable/function is accessible only from inside the compilation unit.
So a static function cannot be called from outside the compilation unit but an external one can:
Static functions cannot be called from outside the compilation unit:
The meaning of the "static" keyword in C depends on whether or not it appears within a brace-delimited block. Refer to the following code snippet.
int a;
static int b;
int func(void){
int c;
static int d;
...
}
Variables 'a' and 'b' are declared outside of any brace-delimited blocks, therefore they both persist for the life of the program and are accessible from anywhere in the compilation unit. In this case, the "static" keyword has the effect of making variable 'b' local to the compilation unit. Variable 'a' can be accessed from any other compilation units that contain an extern declaration for it.Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only accessible from within that block. In this case, the "static" keyword has the effect of making variable 'd' persist for the life of the program. Variable 'c' only persists while execution is within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the same location for successive executions of the brace-delimited block.
C++ is consistent with C as far as the static keyword is concerned. However, in C++ the static keyword is largely deprecated and is in the language generally for backwards compatibility with C. There remain some uses of it, however.
No one has mentioned linkage so far, which is odd. Linkage comes in two flavours, external and internal. External linkage means the variable or function is accessible (shareable) from outside the compilation unit. Internal linkage means the variable/function is accessible only from inside the compilation unit.
So a static function cannot be called from outside the compilation unit but an external one can:
extern void MyFunction(int arg)
{
/* etc... */
}
The function prototype is:
extern void MyFunction(int arg);The extern is the default. External functions (aka global functions) are callable by using the function prototype.
Static functions cannot be called from outside the compilation unit:
static void MyFunction(int arg)
{
/* etc... */
}
The function prototype is:static void MyFunction(int arg);You may place the static function prototype at the top of the file but the definition of the function must reside somewhere in the file. The above applies to variables.
extern int value;This variable is accessible from another compilation unit by:
extern int value;Static variables have internal linkage and are accessible only in the current scope. If the static variable is inside a function, it is accessible only from inside the function. Here the static keyword also causes the local variable to persist after the function completes execution. It's kind of like a local global.
Thứ Năm, 3 tháng 3, 2011
Thứ Hai, 31 tháng 1, 2011
Trộn code C và C++ (Call C++ function from C code and vice versa/ Mixing C and C++)
Làm sao để có thể gọi một hàm C++ từ chương trình C và ngược lại??
Thứ Ba, 4 tháng 1, 2011
Chủ Nhật, 26 tháng 12, 2010
Đăng ký:
Bài đăng (Atom)