Click here to Skip to main content
15,885,546 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How can i create a project that it has the same name with an existing project Pin
November 201326-Mar-13 4:18
November 201326-Mar-13 4:18 
AnswerRe: How can i create a project that it has the same name with an existing project Pin
November 201326-Mar-13 20:13
November 201326-Mar-13 20:13 
GeneralRe: How can i create a project that it has the same name with an existing project Pin
Richard MacCutchan26-Mar-13 6:39
mveRichard MacCutchan26-Mar-13 6:39 
GeneralRe: How can i create a project that it has the same name with an existing project Pin
November 201326-Mar-13 20:11
November 201326-Mar-13 20:11 
GeneralDialog or “dynamic” Menu? Pin
Vaclav_23-Mar-13 4:16
Vaclav_23-Mar-13 4:16 
GeneralRe: Dialog or “dynamic” Menu? Pin
Richard MacCutchan23-Mar-13 6:08
mveRichard MacCutchan23-Mar-13 6:08 
GeneralRe: Dialog or “dynamic” Menu? Pin
pasztorpisti24-Mar-13 4:53
pasztorpisti24-Mar-13 4:53 
QuestionProblem with flockfile and funlockfile Pin
noislude23-Mar-13 4:09
noislude23-Mar-13 4:09 
Hi people. I've been reading Programming with POSIX Threads (I've been learning pthreads by myself) and I always get some errors when I attempt to use flockfile and funlockfile and I don't know how to get rid of these errors. Can you help me? thanks.

C#
/*
 * putchar.c
 *
 * Demonstrate use of stdio file locking to generate an "atomic"
 * sequence of character writes (using putchar). If run with an
 * argument of "1", or no argument, the program uses a sequence
 * of putchar_unlocked calls within flockfile/funlockfile to
 * ensure that one threads writes cannot be interleaved with
 * another's.
 *
 * With an argument of "0", the program uses putchar, without
 * file locks, to show that the writes may be interleaved.
 *
 * The putchar[_unlocked] loop is punctuated with sleep(1) calls
 * to ensure that the desired behavior is demonstrated. Without
 * some delay, even on a multiprocessor the program may often
 * fail to display the interleaved output in this simplified
 * case.
 *
 * With file locking, you can expect to see the following output:
 *
 *      thread 1
 *      thread 2
 *      thread 3
 *
 * While without file locking, you can expect to see something
 * much less predictable, but probably resembling this:
 *
 *      ttthhhiiisss   iiisss   ttthhhrrreeeaaaddd   123
 *
 */
#include <pthread.h>
#include "errors.h"

/*
 * This function writes a string (the function's arg) to stdout,
 * by locking the file stream and using putchar_unlocked to
 * write each character individually.
 */
void *lock_routine (void *arg)
{
    char *pointer;

    flockfile (stdout);
    for (pointer = arg; *pointer != '\0'; pointer++) {
        putchar_unlocked (*pointer);
        sleep (1);
    }
    funlockfile (stdout);
    return NULL;
}

/*
 * This function writes a string (the function's arg) to stdout,
 * by using putchar to write each character individually.
 * Although the internal locking of putchar prevents file stream
 * corruption, the writes of various threads may be interleaved.
 */
void *unlock_routine (void *arg)
{
    char *pointer;

    for (pointer = arg; *pointer != '\0'; pointer++) {
        putchar (*pointer);
        sleep (1);
    }
    return NULL;
}

int main (int argc, char *argv[])
{
    pthread_t thread1, thread2, thread3;
    int flock_flag = 1;
    void *(*thread_func)(void *);
    int status;

    if (argc > 1)
        flock_flag = atoi (argv[1]);
    if (flock_flag)
        thread_func = lock_routine;
    else
        thread_func = unlock_routine;
    status = pthread_create (
        &thread1, NULL, thread_func, "this is thread 1\n");
    if (status != 0)
        err_abort (status, "Create thread");
    status = pthread_create (
        &thread2, NULL, thread_func, "this is thread 2\n");
    if (status != 0)
        err_abort (status, "Create thread");
    status = pthread_create (
        &thread3, NULL, thread_func, "this is thread 3\n");
    if (status != 0)
        err_abort (status, "Create thread");
    pthread_exit (NULL);
}


after running valgrind --tool=helgrind --tool=drd

SQL
=5790== Thread 3:
==5790== Conflicting load by thread 3 at 0x053fb7e8 size 8
==5790==    at 0x4E4D063: flockfile (in /lib64/libpthread-2.15.so)
==5790==    by 0x4009B6: lock_routine (putchar.c:20)
==5790==    by 0x4C2D231: ??? (in /usr/lib64/valgrind/vgpreload_drd-amd64-linux.so)
==5790==    by 0x4E45E0D: start_thread (pthread_create.c:305)
==5790== Allocation context: BSS section of /lib64/libc-2.15.so
==5790== Other segment start (thread 2)
==5790==    at 0x513E291: clone (clone.S:84)
==5790== Other segment end (thread 2)
==5790==    at 0x510FCAD: ??? (syscall-template.S:82)
==5790==    by 0x510FB50: sleep (sleep.c:138)
==5790==    by 0x4009DB: lock_routine (putchar.c:24)
==5790==    by 0x4C2D231: ??? (in /usr/lib64/valgrind/vgpreload_drd-amd64-linux.so)
==5790==    by 0x4E45E0D: start_thread (pthread_create.c:305)
==5790==
Create thread 3 "putchar.c": 64 Success
t==5790==
==5790== For counts of detected and suppressed errors, rerun with: -v
==5790== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 38 from 38)

Question(ask) need help how to divide a file into some file Pin
moonstalker23-Mar-13 3:49
moonstalker23-Mar-13 3:49 
AnswerRe: (ask) need help how to divide a file into some file Pin
dusty_dex23-Mar-13 4:36
dusty_dex23-Mar-13 4:36 
GeneralRe: (ask) need help how to divide a file into some file Pin
moonstalker23-Mar-13 8:53
moonstalker23-Mar-13 8:53 
AnswerRe: (ask) need help how to divide a file into some file Pin
ramrooney26-Mar-13 7:39
ramrooney26-Mar-13 7:39 
SuggestionRe: (ask) need help how to divide a file into some file Pin
Shaheed Legion27-Mar-13 0:48
Shaheed Legion27-Mar-13 0:48 
QuestionHelp needed to print the value nth number raised to the power n. Pin
Rajdeep_22-Mar-13 21:46
Rajdeep_22-Mar-13 21:46 
AnswerRe: Help needed to print the value nth number raised to the power n. Pin
Richard MacCutchan22-Mar-13 23:47
mveRichard MacCutchan22-Mar-13 23:47 
GeneralRe: Help needed to print the value nth number raised to the power n. Pin
Rajdeep_23-Mar-13 0:08
Rajdeep_23-Mar-13 0:08 
AnswerRe: Help needed to print the value nth number raised to the power n. Pin
V.J.NAGA VARA PRASAD23-Mar-13 4:36
V.J.NAGA VARA PRASAD23-Mar-13 4:36 
AnswerRe: Help needed to print the value nth number raised to the power n. Pin
Vaclav_23-Mar-13 4:52
Vaclav_23-Mar-13 4:52 
GeneralRe: Help needed to print the value nth number raised to the power n. Pin
Richard MacCutchan23-Mar-13 6:00
mveRichard MacCutchan23-Mar-13 6:00 
SuggestionRe: Help needed to print the value nth number raised to the power n. Pin
David Crow23-Mar-13 16:05
David Crow23-Mar-13 16:05 
QuestionDirectX SDK – which version? Pin
Vaclav_22-Mar-13 8:38
Vaclav_22-Mar-13 8:38 
AnswerRe: DirectX SDK – which version? Pin
dusty_dex22-Mar-13 8:45
dusty_dex22-Mar-13 8:45 
QuestionUse InternetSetOption() to change proxy Pin
Oren_Davod21-Mar-13 23:03
Oren_Davod21-Mar-13 23:03 
AnswerRe: Use InternetSetOption() to change proxy Pin
Richard MacCutchan21-Mar-13 23:38
mveRichard MacCutchan21-Mar-13 23:38 
Generalc++ queue OUTPUT ? Pin
kr kumar21-Mar-13 9:33
kr kumar21-Mar-13 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.