sharedseg.cpp

来自「window下的多线程编程参考书。值得一读」· C++ 代码 · 共 38 行

CPP
38
字号
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#define MAX_STRING_SIZE     256
#define INITIAL_STRING      __TEXT("(nothing yet)")

#pragma data_seg(".Shared")
TCHAR g_szSharedString[MAX_STRING_SIZE] = INITIAL_STRING;
#pragma data_seg()

int main( int argc, char *argv[]) {
    TCHAR szLocalString[MAX_STRING_SIZE];

    while (TRUE) {
        printf( "Type a string to share, [Enter] to display current string, or \"quit\":\n");
        
        // input string...
        _getts( szLocalString);
        
        if (_tcscmp( szLocalString, __TEXT("quit")) == 0) {
            // quit...
            break;
        }
        else if (szLocalString[0] == __TEXT('\0')) {
            // show the string...
            printf( "Current string is '%s'.\n", g_szSharedString);
        }
        else {
            // set the string...
            _tcscpy( g_szSharedString, szLocalString);
        }
    }

    // exit...
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?